[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 ABS(IEXP) (INTEGER)

 Function
  Returns the absolute value of an integer expression.

 Syntax
  ABS(iexp)

   iexp = Any integer expression.

 Return Type & Value
  INTEGER
  If iexp is greater than or equal to 0, this function returns
  iexp.  Otherwise this function returns -iexp.

 Remarks
  The most significant use of the absolute value function is to determine
  the difference between two values.  For example, you may need to know in
  a program the difference between 8 and 13.  Normal subtraction would
  yield a result of -5 (8-13).

  You don't need the mathematical difference though, you need the logical
  difference between the two integers.  The absolute value function will
  return that.  In other words, while 8-13 is -5, ABS(8-13) is 5, which may
  be a more desirable result in many cases.

  Also, it is easier to code and understand than this:

  INTEGER D
  LET D = 8-13
  IF (D < 0) LET D = -D

 Examples
  INTEGER num
  ' Loop while num is < 6 or num > 10
  ' ... ABS(4-8)=4 ABS( 5-8)=3 ABS( 6-8)=2 ABS( 7-8)=1
  '     ABS(9-8)=1 ABS(10-8)=2 ABS(11-8)=3 ABS(12-8)=4 ...
  WHILE (ABS(num-8) > 2) DO
    PRINTLN "Enter a number from 6 to 10:"
    INPUT "Number",num
  ENDWHILE
  INTEGER i, r
  ' Generate 10 random numbers from -5 to 5
  ' Print each number and it's absolute value
  FOR i = 1 TO 10
    LET r = RANDOM(10)-5
    PRINTLN "The absolute value of ",r," is ",ABS(r)
  NEXT

See Also:
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson